#include "game.h"

int x = 0;
int y = 0;


void Menu(){

	printf("#####################\n");
	printf("## 1.play   0.exit ##\n");
	printf("#####################\n");
	printf("please select# ");

	return ;
}


int ChessCount(int board[][Col],int row,int col,enum Dir d)
{
    int _x = x - 1;   //cong yi kai shi
    int _y = y - 1;   //cong yi kai shi
    
    int count = 0;
    while(1)
    {
        switch(d)
        {
            case LEFT:
                _y--;
                break;
            case RIGHT:
                _y++;
                break;
            case UP:
                _x--;
                break;
            case DOWN:
                _x++;
                break;
            case LEFT_UP:
                _x--,_y--;
                break;
            case RIGHT_DOWN:
                _x++,_y++;
                break;
            case RIGHT_UP:
                _x--,_y++;
                break;
            case LEFT_DOWN:
                _x++,_y--;
                break;
        }

        if(_x < 0 || _x > row - 1 || _y < 0 || _y > col - 1)
        {
            break;
        }

        if(board[x - 1][y - 1] == board[_x][_y])
        {
            count++;
        }

        else
        {
            break;
        }
    }
    
    return count;

}



int IsOver(int board[][Col],int row,int col)
{ 
    

    int count1 = ChessCount(board,row,col,LEFT) + ChessCount(board,row,col,RIGHT) + 1;
    int count2 = ChessCount(board,row,col,UP) + ChessCount(board,row,col,DOWN) + 1;
    int count3 = ChessCount(board,row,col,LEFT_UP) + ChessCount(board,row,col,RIGHT_DOWN) + 1;
    int count4 = ChessCount(board,row,col,LEFT_DOWN) + ChessCount(board,row,col,RIGHT_UP) + 1;

    if(count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5)
    {
        return board[x-1][y-1];
    }

    int i = 0;
    for(;i<row;i++)
    {
        int j = 0;
        for(;j<col;j++)
        {
            if(board[i][j] == 0)
            {
                return NEXT;
            }
        }
    }

    return DRAW;
}


void ShowBoard(int board[Row][Col])
{


    printf("  ");
    int i = 1;
    for(;i <= Col;i++)
    {
        printf("%3d",i);
    }
    printf("\n");
    i = 0;
    for(;i < Row;i++)
    {
        printf("%2d ",i+1);
        int j = 0;
        for(;j < Col;j++)
        {
            if(board[i][j] == 0)
            {
                printf(" . ");
            }
            else if(board[i][j] == PLAYER1)
            {
                printf(" x ");
            }
            else
            {
                printf(" o ");
            }
        }
        printf("\n");
    }
}

	
void PlayerMove(int board[][Col],int row,int col,int who)
{

    while(1)
    {
        printf("Player[%d] Please Enter Your Pos# ",who);
        scanf("%d %d",&x,&y);
        if(x<1 || x>row || y<1 || y>col)
        {
             printf("Pos Is Not Rigth!\n");
             //continue;
        }
        else if(board[x-1][y-1] != 0)
        {
            printf("Pos Is Occupied!\n");
            //continue;
        }
        else
        {
            board[x-1][y-1] = who;
            break;
        }

    }


}

void Game(){
	
	int board[Row][Col];
	memset(board , '\0' , sizeof(board));
	int result = NEXT;
  do{
        ShowBoard(board);


		    PlayerMove(board,Row,Col,PLAYER1);
    		result = IsOver(board,Row,Col);
		    if(NEXT != result)
        {
			      break;
		    }

        ShowBoard(board);
		    PlayerMove(board,Row,Col,PLAYER2);
		    result = IsOver(board,Row,Col);
		    if(NEXT != result)
        {
			      break;
		    }

	  }while(1);
  
    ShowBoard(board);
    switch(result)
    {
        case PLAYER1_WIN:
            printf("1 win!\n");
            break;
        case PLAYER2_WIN:
            printf("2 win!\n");
            break;
        case DRAW:
            printf("pj!\n");
            break;
        default:
            break;
    }
}



